package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.ArticlePageRequestDTO;
import com.hui.admin.dto.request.ArticleSaveRequestDTO;
import com.hui.admin.dto.response.ArticleCategoryResponseDTO;
import com.hui.admin.dto.response.ArticlePageResponseDTO;
import com.hui.admin.service.ArticleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 文章控制层
 */
@Slf4j
@RestController
@RequestMapping("/api/article")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    /**
     * 获取文章列表
     */
    @GetMapping
    public Result<ArticlePageResponseDTO> getArticleList(ArticlePageRequestDTO requestDTO) {
        return Results.success(articleService.getArticleList(requestDTO));
    }

    /**
     * 新增文章
     */

    @Log("新增删除文章")
    @PostMapping
    public Result<Void> createArticle(@RequestBody ArticleSaveRequestDTO requestDTO) {
        articleService.createArticle(requestDTO);
        return Results.success();
    }

    /**
     * 修改文章
     */
    @Log("修改文章")
    @PutMapping
    public Result<Void> updateArticle(@RequestBody ArticleSaveRequestDTO requestDTO) {
        articleService.updateArticle(requestDTO);
        return Results.success();
    }

    /**
     * 删除文章
     */
    @Log("删除文章")
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return Results.success();
    }

    /**
     * 批量删除文章
     */
    @Log("批量删除文章")
    @PostMapping("/deleteBatch")
    public Result<Void> deleteArticles(@RequestBody Map<String, Long[]> payload) {
        Long[] ids = payload.get("ids");
        articleService.deleteArticles(ids);
        return Results.success();
    }

    /**
     * 获取分类列表
     */
    @GetMapping("/category")
    public Result<List<ArticleCategoryResponseDTO>> getCategoryList() {
        return Results.success(articleService.getCategoryList());
    }

}
